/*    This file is part of Nephite Exchange.
 *
 *    Nephite Exchange is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    Nephite Exchange is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with Nephite Exchange.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.icub3d.nephiteexchange.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Nephite_exchange implements EntryPoint, ValueChangeHandler<String> {

	/* These are panels that are used by the class. */
	private VerticalPanel sitePanel = new VerticalPanel();
	private VerticalPanel mainPanel = new VerticalPanel();

	/* This will be used globally in the class. */
	private Label errorLabel = new Label();
	private LastUpdatedLabel lastUpdatedLabel = new LastUpdatedLabel(errorLabel);

	
	/* These are the different panels for the different links. */
	private ConvertPanel convertPanel = new ConvertPanel(errorLabel);
	private ExchangesPanel exchangesPanel = new ExchangesPanel(errorLabel);
	private AboutPanel aboutPanel = new AboutPanel();
	private ContactPanel contactPanel = new ContactPanel(errorLabel);
	private LinksPanel linksPanel = new LinksPanel();
	
	/**
	 * This is the entry point method.
	 */
	public void onModuleLoad() {

		createMainLayout();
		
		RootPanel.get("nephiteExchange").add(sitePanel);

		sitePanel.setStyleName("sitePanel");
		mainPanel.setStyleName("mainPanel");
		
		String initToken = History.getToken();
		if (initToken.length() == 0) {
			History.newItem("convert");
		}

		History.addValueChangeHandler(this);

		History.fireCurrentHistoryState();
	}

	private void createMainLayout() {
		sitePanel.add(linksPanel);
		sitePanel.add(errorLabel);
		sitePanel.add(mainPanel);
		sitePanel.add(lastUpdatedLabel);
	}
	
	@Override
	public void onValueChange(ValueChangeEvent<String> event) {
		String value = event.getValue();
		
		/* Change highlight. */
		linksPanel.changeLink(value);
		
		if (value.equals("convert")) {
			mainPanel.clear();
			mainPanel.add(convertPanel);
		} else if (value.equals("exchanges")) {
			mainPanel.clear();
			mainPanel.add(exchangesPanel);
		} else if (value.equals("contact")) {
			mainPanel.clear();
			mainPanel.add(contactPanel);
		} else if (value.equals("about")) {
			mainPanel.clear();
			mainPanel.add(aboutPanel);
		}
	}


}
